<template>
  <a-modal
    title="样品"
    width="70%"
    :visible="visible"
    :confirmLoading="loading"
    okText="选择"
    @ok="handleChoose"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-card :bordered="false">
      <div class="table-operator">
        <a-button type="primary" style="margin-bottom:8px" icon="redo" @click="getDataList()">刷新</a-button>
      </div>

      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="10">
            <a-col :md="4" :sm="24">
              <a-form-item label="查询类别">
                <a-select allowClear v-model="queryParam.condition">
                  <a-select-option key="CustomerName">客户名称</a-select-option>

                  <a-select-option key="DocumentNo">单据号</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-input v-model="queryParam.keyword" placeholder="关键字" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button
                type="primary"
                @click="
                  () => {
                    this.pagination.current = 1
                    this.getDataList()
                  }
                "
                >查询</a-button
              >
              <a-button style="margin-left: 8px" @click="repossess">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table
        ref="table"
        :scroll="{ y: scroll }"
        :columns="columns"
        :rowKey="(row, index) => row.index"
        :dataSource="data"
        :pagination="pagination"
        :loading="loading"
        @change="handleTableChange"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: type }"
        :bordered="true"
        size="small"
      >
        <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      </a-table>

      <!-- <edit-form ref="editForm" :parentObj="this"></edit-form> -->
    </a-card>
  </a-modal>
</template>

<script>
// import EditForm from './EditForm'
// import List from '../Cust_CustomerLinkMan/List.vue'
import moment from 'moment'
const columns = [
  { title: '客户名称', dataIndex: 'CustomerName', width: '15%' },
  { title: '单据号', dataIndex: 'DocumentNo' },
  {
    title: '申请日期',
    dataIndex: 'CreateTime',

    align: 'center',
    scopedSlots: { customRender: 'CreateTime' }
  },
  { title: '销售经理', dataIndex: 'AccountManager' },

  { title: '收货地址', dataIndex: 'DeliverAddress', width: '20%' }
]
export default {
  components: {
    // EditForm,
    // List
  },
  props: {
    type: { type: String, default: 'radio', required: false }
  },

  created() {},
  computed: {
    scroll() {
      // //console.log()
      return Number(document.documentElement.clientHeight) * 0.45
    }
  },
  data() {
    return {
      flag: false,
      user: 1,
      data: [],
      list: [], // 省级
      listChildren: [], // 市
      county: [], // 县
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: [{ condition: 'CustomerName' }],
      selectedRows: [],
      selectedRowKeys: [],
      visible: false
    }
  },
  methods: {
    repossess() {
      this.queryParam = [{ condition: 'CustomerName' }]
      this.getDataList()
    },
    userChange(n, b) {
      this.getDataList()
    },

    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    getDataList() {
      this.selectedRowKeys = []

      this.loading = true
      if (this.flag) {
        this.$http

          .post('/SampleManagement/SPL_SampleApplyMain/GetDataListByMoreCondition', {
            PageIndex: this.pagination.current,
            PageRows: this.pagination.pageSize,
            SortField: this.sorter.field || 'Id',
            SortType: this.sorter.order,
            Search: this.queryParam,
            ...this.filters
          })
          .then(resJson => {
            this.loading = false
            this.data = resJson.Data
            if (this.data) {
              const pagination = { ...this.pagination }
              pagination.total = resJson.Total
              this.pagination = pagination
            }
          })
      } else {
        this.$http

          .post('/SampleManagement/SPL_SampleApplyMain/GetDataListByMySelfAndMoreCondition', {
            PageIndex: this.pagination.current,
            PageRows: this.pagination.pageSize,
            SortField: this.sorter.field || 'Id',
            SortType: this.sorter.order,
            Search: this.queryParam,
            ...this.filters
          })
          .then(resJson => {
            this.loading = false
            this.data = resJson.Data
            if (this.data) {
              const pagination = { ...this.pagination }
              pagination.total = resJson.Total
              this.pagination = pagination
            }
          })
      }
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },

    hasSelected() {
      return this.selectedRowKeys.length > 0
    },

    handleDelete(ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk() {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/CustomerManagement/Cust_CustomerInfoFormal/DeleteData', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    openChoose(flag) {
      this.queryParam = [{ condition: 'CustomerName' }]
      this.flag = flag
      this.pagination.current = 1

      this.getDataList()

      this.visible = true
    },
    handleChoose() {
      this.visible = false
      console.log(this.selectedRows)
      this.$emit('onSample', this.selectedRows)
      this.queryParam = [{ condition: 'CustomerName' }]
      this.pagination.current = 1

      this.getDataList()
    },

    timeFormat(val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>
